<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="第七章 流和文件7.1文件管理基础 1.File构造方法  File(File parent, String child)  从父抽象路径名和子路径名字符串创建新的 File实例；   File(String pathname)  通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例；   File(String parent, String child)  从父路径名字符串和子路径名">
<meta property="og:type" content="article">
<meta property="og:title" content="Hexo">
<meta property="og:url" content="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E4%B8%83%E7%AB%A0%20%E6%B5%81%E5%92%8C%E6%96%87%E4%BB%B6/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="第七章 流和文件7.1文件管理基础 1.File构造方法  File(File parent, String child)  从父抽象路径名和子路径名字符串创建新的 File实例；   File(String pathname)  通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例；   File(String parent, String child)  从父路径名字符串和子路径名">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-12-19T17:32:32.783Z">
<meta property="article:modified_time" content="2021-12-19T17:32:34.745Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/notes-on-computer-expertise/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/notes-on-computer-expertise/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/notes-on-computer-expertise/css/style.css">

  
    
<link rel="stylesheet" href="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 5.4.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/notes-on-computer-expertise/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/">Home</a>
        
          <a class="main-nav-link" href="/notes-on-computer-expertise/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/notes-on-computer-expertise/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://goofyer.gitee.io/notes-on-computer-expertise"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-Java/第七章 流和文件" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E4%B8%83%E7%AB%A0%20%E6%B5%81%E5%92%8C%E6%96%87%E4%BB%B6/" class="article-date">
  <time class="dt-published" datetime="2021-12-19T17:32:32.783Z" itemprop="datePublished">2021-12-20</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="第七章-流和文件"><a href="#第七章-流和文件" class="headerlink" title="第七章 流和文件"></a>第七章 流和文件</h2><h3 id="7-1文件管理基础"><a href="#7-1文件管理基础" class="headerlink" title="7.1文件管理基础"></a>7.1文件管理基础</h3><ul>
<li><p>1.File构造方法</p>
<ul>
<li><p>File(File parent, String child)</p>
<ul>
<li>从父抽象路径名和子路径名字符串创建新的 File实例；</li>
</ul>
</li>
<li><p>File(String pathname)</p>
<ul>
<li>通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例；</li>
</ul>
</li>
<li><p>File(String parent, String child)</p>
<ul>
<li>从父路径名字符串和子路径名字符串创建新的 File实例；</li>
</ul>
</li>
</ul>
</li>
<li><p>2.获取路径或文件名</p>
<ul>
<li><p>String getName()</p>
<ul>
<li>返回文件或目录的名称</li>
</ul>
</li>
<li><p>String getPath()</p>
<ul>
<li>返回路径名（构造时候的路径）</li>
</ul>
</li>
<li><p>String getAbsolutePath()</p>
<ul>
<li>返回绝对路径名（全路径）</li>
</ul>
</li>
<li><p>String getCanonicalPath()</p>
<ul>
<li>返回路径名的规范形式</li>
</ul>
</li>
<li><p>String getParent()</p>
<ul>
<li>返回父目录的路径名</li>
</ul>
</li>
</ul>
</li>
<li><p>3.读取属性</p>
<ul>
<li><p>boolean canExecute()</p>
<ul>
<li>测试是否为可执行文件</li>
</ul>
</li>
<li><p>boolean exists()</p>
</li>
<li>boolean canWrite()</li>
<li>boolean canRead()</li>
<li>boolean isHidden()</li>
<li>boolean isFile()</li>
<li>boolean isDirectory()</li>
<li><p>Long lastModeified()</p>
<ul>
<li>获取文件最近一次修改时间</li>
</ul>
</li>
<li><p>Long length()</p>
</li>
</ul>
</li>
<li><p>4.设置属性</p>
<ul>
<li>Long setlastModeified()</li>
<li>boolean setExecutable()</li>
<li>boolean exists()</li>
<li>boolean setWritable()</li>
<li>boolean setReadable()</li>
<li>boolean setReadOnly()</li>
</ul>
</li>
<li><p>5.文件和目录操作</p>
</li>
<li><p>7.1.2案例7-1递归显示或删除文件</p>
<ul>
<li>技术</li>
<li>使用file对象的isfile()判断是否为文件，如果是文件直接进行显示或删除操作，否则，在使用isDirectory判断是否为目录，是目录通过listFiles获得文件或者子目录 递归</li>
<li>步骤</li>
<li><p>第一步 建立DemoFile类</p>
<ul>
<li>public class DemoFile {<br>private static void deleteFile(File file) {<br>if (file != null) {<br>  if (file.isFile())<pre><code>  file.delete();
</code></pre>  else if (file.isDirectory()) {<pre><code>  File files[] = file.listFiles();
  for (int i = 0; i &lt; files.length; i++)
      deleteFile(files[i]);
  file.delete();
</code></pre>  }<br>}<br>}</li>
</ul>
<p>private static void listFile(File file, int level)<br>{<br>  if (file != null)<br>  {</p>
<pre><code>  if (file.isFile())
      System.out.println(String.format(&quot;&quot;));
  else if (file.isDirectory()) &#123;
      File files[] = file.listFiles();
      System.out.println(String.format(&quot;&quot;));
      for (int i = 0; i &lt; files.length; i++);
      listFile(files[i],level+1);
  &#125;
</code></pre><p>  }<br>}<br>private  static String aaa(int n)<br>{<br>  StringBuilder sb=new StringBuilder();<br>  for(int i=0;i&lt;n;i++)</p>
<pre><code>  sb.append(&quot; &quot;);
</code></pre><p>  return sb.toString();<br>}</p>
<p>public static void main(String[]args)<br>{<br>  File file=new File(“”);<br>  listFile(file,1);<br>  deleteFile(file);<br>  listFile(file,1);<br>}<br>}</p>
</li>
</ul>
</li>
<li><p>7.1.3过滤器与文件选择对话框</p>
<ul>
<li><p>1.FileFilter接口</p>
<ul>
<li><p>MyFilter类为自定义文件过滤器，实现了FileFilter接口，FileFilter接口中包含一个accept（File file）方法，该方法会对指定的File中的子目录和文件进行迭代，如果返回true，则list方法会列出该目录或者文件。</p>
<ul>
<li>class MyFilter implements FileFilter{</li>
</ul>
</li>
</ul>
<p>@Override<br>public boolean accept(File pathname) {<br>  return pathname.getName().endsWith(“.jar”)&amp;&amp;pathname.isFile();<br>}</p>
</li>
</ul>
</li>
</ul>
<p>}</p>
<pre><code>- 2.FilenameFilter接口

    - 一、FilenameFilter介绍

        - Java.io.FilenameFilter是文件名过滤器，用来过滤不符合规格的文件名，并返回合格的文件；
        - 一般地：

            - (1)String[] fs = f.list();
            - (2)File[] fs = f.listFiles();
            - 这两个方法返回f下的所有文件或目录；

                - FilenameFilter用来把符合要求的文件或目录返回；
                - 因此可以调用：

                    - (1)String []fs = f.list(FilenameFilter filter);;
                    - (2)File[]fs = f.listFiles(FilenameFilter filter);

    - 二、FilenameFilter的使用举例

        - //System.getProperty(&quot;catalina.home&quot;)获取到Tomcat的根路径，比如：D:\Program Files (x86)\tomcat\apache-tomcat-7.0.70-windows-x86\apache-tomcat-7.0.70
</code></pre><p>File dataDirectory = new File(System.getProperty(“catalina.home”) + File.separator + “test” + File.separator + “conf”);// 这里获取的是：D:\Program Files (x86)\tomcat\apache-tomcat-7.0.70-windows-x86\apache-tomcat-7.0.70\test\conf<br>        // 数据源文件必定要是properties类型。<br>        File[] dataFiles = dataDirectory.listFiles(new FilenameFilter() {<br>//以config_db开头，文件后缀为.properties的将被选出来，其余被过滤掉<br>            @Override<br>            public boolean accept(File dir, String name) {<br>                String fileName = name.toLowerCase();</p>
<pre><code>            if (fileName.endsWith(&quot;.properties&quot;)) &#123;
                if(fileName.startsWith(&quot;config_db&quot;)) &#123;
                    return true;
                &#125;
            &#125;
            return false;
        &#125;
    &#125;);
</code></pre><h3 id="7-2字符流与文本文件读写"><a href="#7-2字符流与文本文件读写" class="headerlink" title="7.2字符流与文本文件读写"></a>7.2字符流与文本文件读写</h3><ul>
<li>字符流以字符为基本单位处理数据</li>
<li><p>7.2.1字符流简介</p>
<ul>
<li>从reader与writer派生</li>
<li><p>字符流的层次结构</p>
<ul>
<li><p>Reader</p>
<ul>
<li><p>BufferedReader</p>
<ul>
<li>LineNumberReader</li>
</ul>
</li>
<li><p>CharArrayReader</p>
</li>
<li><p>InputStreamReader</p>
<ul>
<li>FileReader</li>
</ul>
</li>
<li><p>FilterReader</p>
<ul>
<li>PushbackReader</li>
</ul>
</li>
<li><p>PipedReader</p>
</li>
<li>StringReader</li>
</ul>
</li>
<li><p>Writer</p>
<ul>
<li>BufferedWriter</li>
<li>CharArrayWriter</li>
<li><p>OutputStreamWriter</p>
<ul>
<li>FileWriter</li>
</ul>
</li>
<li><p>FilterWriter</p>
</li>
<li>PiperWriter</li>
<li>StringWriter</li>
<li>PrintWriter</li>
</ul>
</li>
</ul>
</li>
<li><p>1.Reader类</p>
<ul>
<li>abstract  void  close() //关闭该流并释放与之关联的所有资源。</li>
<li>void  mark(int readAheadLimit) //标记流中的当前位置。</li>
<li>boolean  markSupported() //判断此流是否支持 mark() 操作。</li>
<li>int  read() //读取单个字符。</li>
<li>int  read(char[] cbuf) //将字符读入数组。</li>
<li>abstract  int  read(char[] cbuf, int off, int len) //将字符读入数组的某一部分。</li>
<li>int  read(CharBuffer target) //试图将字符读入指定的字符缓冲区。</li>
<li>boolean  ready() //判断是否准备读取此流。</li>
<li>void  reset() //重置该流。</li>
<li>long  skip(long n) //跳过字符。</li>
</ul>
</li>
<li><p>2 writer类</p>
<ul>
<li>Writer  append(char c) 将指定字符添加到此 writer。</li>
<li>Writer  append(CharSequence csq) 将指定字符序列添加到此 writer。</li>
<li>Writer  append(CharSequence csq, int start, int end) 将指定字符序列的子序列添加到此 writer.Appendable。</li>
<li>abstract  void  close() 关闭此流，但要先刷新它。</li>
<li>abstract  void  flush() 刷新该流的缓冲。</li>
<li>void  write(char[] cbuf) 写入字符数组。</li>
<li>abstract  void  write(char[] cbuf, int off, int len) 写入字符数组的某一部分。</li>
<li>void  write(int c) 写入单个字符。</li>
<li>void  write(String str) 写入字符串。</li>
<li>void  write(String str, int off, int len) 写入字符串的某一部分。</li>
</ul>
</li>
</ul>
</li>
<li><p>7.2.2文件字符流</p>
<ul>
<li>1 文件读字符流</li>
<li>2 文件写字符流</li>
<li>fileReader</li>
<li>fileWriter</li>
</ul>
</li>
</ul>
<h3 id="7-4-数据流和对象流"><a href="#7-4-数据流和对象流" class="headerlink" title="7.4 数据流和对象流"></a>7.4 数据流和对象流</h3><ul>
<li><p>7.4.1 数据流简介</p>
<ul>
<li>数据流易于机器无关的方式读取和写数据，这里数据指的是JAVA的基本数据类型和string 基本数据类型包括byte int char long float double boolean 和short</li>
</ul>
</li>
<li><p>7.4.2对象流简介</p>
</li>
<li>java通关对象的串行化来实现保存对象的功能</li>
</ul>
<h3 id="子主题-6"><a href="#子主题-6" class="headerlink" title="子主题 6"></a>子主题 6</h3>
      
    </div>
    <footer class="article-footer">
      <a data-url="https://goofyer.gitee.io/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E4%B8%83%E7%AB%A0%20%E6%B5%81%E5%92%8C%E6%96%87%E4%BB%B6/" data-id="cl403sxr00013f8vu56642j1u" data-title="" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%85%AB%E7%AB%A0%20%E5%A4%9A%E7%BA%BF%E7%A8%8B/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          (no title)
        
      </div>
    </a>
  
  
    <a href="/notes-on-computer-expertise/2021/12/20/Java/%E7%AC%AC%E5%85%AD%E7%AB%A0%20java%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BC%96%E7%A8%8B/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title"></div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C#工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/C-%E5%B7%A5%E5%85%B7/">C++工具</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/Mysql/">Mysql</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/blender/">blender</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/linux/">linux</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/manim/">manim</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/spring/">spring</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C/">vba基础操作</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E5%88%9B%E9%80%A0%E6%A8%A1%E5%BC%8F/">创造模式</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">操作系统基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E5%BC%80%E5%8F%91/">操作系统开发</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E5%AD%A6/">数学</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/">数据结构</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/">数据结构基础知识</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%A8%A1%E6%9D%BF/">模板</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%8B%B1%E8%AF%AD/">英语</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%99%9A%E5%B9%BB4/">虚幻4</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BB%84%E6%88%90%E5%8E%9F%E7%90%86/">计算机组成原理</a></li><li class="category-list-item"><a class="category-list-link" href="/notes-on-computer-expertise/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C/">计算机网络</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/notes-on-computer-expertise/tags/C/" rel="tag">C++</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/notes-on-computer-expertise/tags/C/" style="font-size: 10px;">C++</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/03/">March 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/notes-on-computer-expertise/archives/2021/12/">December 2021</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/31/vue/%E5%89%8D%E7%AB%AF%E6%A1%86%E6%9E%B6%E9%AA%8C%E8%AF%81%E7%A0%81%E6%97%A0%E6%B3%95%E6%98%BE%E7%A4%BA/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/26/%E6%95%B0%E6%8D%AE%E5%BA%93/navicat%E5%AE%89%E8%A3%85/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/25/%E8%99%9A%E5%B9%BB4%E5%BC%95%E6%93%8E%E5%BC%80%E5%8F%91/%E8%99%9A%E5%B9%BB%E5%9B%9B%20mod%E5%88%B6%E4%BD%9C/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/python/python%E6%89%B9%E9%87%8F%E7%94%9F%E6%88%90%E6%95%B0%E6%8D%AE/">(no title)</a>
          </li>
        
          <li>
            <a href="/notes-on-computer-expertise/2022/05/23/vba%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C%E7%AC%94%E8%AE%B0/EXCEL%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2022 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/notes-on-computer-expertise/" class="mobile-nav-link">Home</a>
  
    <a href="/notes-on-computer-expertise/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/notes-on-computer-expertise/js/jquery-3.4.1.min.js"></script>



  
<script src="/notes-on-computer-expertise/fancybox/jquery.fancybox.min.js"></script>




<script src="/notes-on-computer-expertise/js/script.js"></script>





  </div>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</body>
</html>